//
//  IMNet.m
//  SportSocial
//
//  Created by yull on 14/12/10.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "IMNet.h"

@implementation IMNet

+ (AFHTTPRequestOperation *)getIMServerWithSuccessBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSString *url = [NSString stringWithFormat:@"%@/user/getChatServer",[self baseUrl]];
    
    return [[self manager] GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock(operation,error);
    }];
}

+ (AFHTTPRequestOperation *)sendPrivateAudioWithData:(NSData *)audioData
                                        receiveId:(UInt64)receiveId
                                        successBlock:(httpSuccessBlock)successBlock
                                           failBlock:(httpFailBlock)failBlock
{
    NSString *url = [NSString stringWithFormat:@"%@/im/file_upload/uploadVoice?token=%@&user_id=%lld",[self baseUrl], [self token], receiveId];
    return [[self manager] POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:audioData name:@"audio" fileName:@"audio.amr" mimeType:@"audio/amr"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"上传语音返回结果:%@",responseObject);
        NSString *message = [self handleResultCode:[[responseObject objectForKey:@"resultCode"] integerValue]];
        if (message.length == 0) {
            successBlock (operation,responseObject);
        }else {
            failBlock (operation,nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
}

+ (AFHTTPRequestOperation *)sendPrivatePicWithData:(NSData *)picData
                                         receiveId:(UInt64)receiveId
                                      successBlock:(httpSuccessBlock)successBlock
                                         failBlock:(httpFailBlock)failBlock
{
    NSString *url = [NSString stringWithFormat:@"%@/im/file_upload/uploadImage?token=%@&user_id=%lld",[self baseUrl], [self token], receiveId];
    return [[self manager] POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:picData name:@"pic" fileName:@"pic.jpg" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"上传图片返回结果:%@",responseObject);
        NSString *message = [self handleResultCode:[[responseObject objectForKey:@"resultCode"] integerValue]];
        if (message.length == 0) {
            successBlock (operation,responseObject);
        }else {
            failBlock (operation,nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
}

+ (AFHTTPRequestOperation *)sendGroupPicWithData:(NSData *)picData
                                         groupId:(UInt64)groupId
                                    successBlock:(httpSuccessBlock)successBlock
                                       failBlock:(httpFailBlock)failBlock
{
    NSString *url = [NSString stringWithFormat:@"%@/im/file_upload/uploadImage?token=%@&group_id=%lld",[self baseUrl], [self token], groupId];
    return [[self manager] POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:picData name:@"pic" fileName:@"pic.jpg" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"上传图片返回结果:%@",responseObject);
        NSString *message = [self handleResultCode:[[responseObject objectForKey:@"resultCode"] integerValue]];
        if (message.length == 0) {
            successBlock (operation,responseObject);
        }else {
            failBlock (operation,nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
}

+ (AFHTTPRequestOperation *)sendGroupAudioWithData:(NSData *)audioData groupId:(UInt64)groupId successBlock:(httpSuccessBlock)successBlock failBlock:(httpFailBlock)failBlock
{
    NSString *url = [NSString stringWithFormat:@"%@/im/file_upload/uploadVoice?token=%@&group_id=%lld",[self baseUrl], [self token], groupId];
    return [[self manager] POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:audioData name:@"audio" fileName:@"audio.amr" mimeType:@"audio/amr"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"上传语音返回结果:%@",responseObject);
        NSString *message = [self handleResultCode:[[responseObject objectForKey:@"resultCode"] integerValue]];
        if (message.length == 0) {
            successBlock (operation,responseObject);
        }else {
            failBlock (operation,nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
}

+ (AFHTTPRequestOperation *)getAudioWithUrl:(NSString *)audioUrl
                               successBlock:(httpSuccessBlock)successBlock
                                  failBlock:(httpFailBlock)failBlock
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/octet-stream",@"video/3gpp",nil];
    
    AFHTTPRequestOperation *operation = [manager GET:audioUrl
                                          parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                              successBlock (operation,responseObject);
                                          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                              failBlock (operation,error);
                                          }];
    return operation;
}
@end
